<?php

#--------------------------------------------配置区 start---------------------------------------------

//每秒允许访问次数
$pre_allow_visit_count=5;
$error_tips='你的访问过于频繁,请稍后尝试';
#--------------------------------------------配置区 end---------------------------------------------


#--------------------------------------------数据连接 start---------------------------------------------

$conn=new Mongo("mongodb://192.168.0.201:27017");
$FW_DB=$conn->site;

$fw_logs=$FW_DB->logs;

#--------------------------------------------数据连接 end---------------------------------------------


//设置标志cookie
session_start();
$session_id = session_id();
if(empty($_COOKIE['_tj']))
{
    $uuid=uniqid('',true);
    setcookie('_tj',$uuid,time()+315360000,'/');
    $_tj=$uuid;
}else{
    $_tj=$_COOKIE['_tj'];
}

//要访问过滤的
$request_uri=$_SERVER[ 'REQUEST_URI' ];
$request_uri=explode('.',$request_uri);
if(is_array($request_uri))
{
    $end_str=end($request_uri);
    if($end_str=='js'||$end_str=='ico'||$end_str=='png'||$end_str=='jpeg'||$end_str=='jpg'||$end_str=='css'){
        return ;
    }
}

//查询访问次数
$from_ip=fw_getIP();
$visit_time=date('Y-m-d H:i:s');
$pre_vist_count=$logs->find(['from_ip'=>$from_ip,'ctime'=>$visit_time])->count();

if($pre_vist_count>=$pre_allow_visit_count)
{
    exit($error_tips);
}

//组织数据
$fw_data = [
//来源
'name'        => empty( $_GET[ 'frm' ] ) ? '' : $_GET[ 'frm' ],
//从哪个主机
'from_host'   => empty( $_SERVER[ 'HTTP_HOST' ] ) ? '' : $_SERVER[ 'HTTP_HOST' ],
//从哪个IP
'from_ip'     => $from_ip,
//用户UA
'user_agent'  => $ua[ 'platform' ] . '|' . $ua[ 'browsers' ],
'ua'=>fw_getUA(),
'os'=>fw_getOS(),
//从哪个页面而来
'referer'     => empty( $_SERVER[ 'HTTP_REFERER' ] ) ? '' : $_SERVER[ 'HTTP_REFERER' ],
//持久化的一个cookie有效期为10年
'cookie_tag'  => $_tj,
//第次会话的session_id
'session_id'  => $session_id,
//用户ID
'user_id'     => empty($_SESSION['user_id'])?0:$_SESSION['user_id'],
//session信息
'session'     => json_encode( $_SESSION, JSON_UNESCAPED_UNICODE ),
//请求的是哪个页面
'request_uri' => $_SERVER[ 'REQUEST_URI' ],
//站点类型，PC,WAP,IOS,ANDROID
'site_type'=>empty($params['site_type'])?'':$params['site_type'],
'ctime'=>date('Y-m-d H:i:s'),
];
$is_ok=$fw_logs->insert($data);



#--------------------------------------------函数区 start---------------------------------------------
//得到来访者IP
function fw_getIP()
{
    $unknown = 'unknown';
    if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)){
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    /**
     * 处理多层代理的情况
     * 或者使用正则方式：$ip = preg_match("/[\d\.]{7,15}/", $ip, $matches) ? $matches[0] : $unknown;
     */
    if (false !== strpos($ip, ',')) $ip = reset(explode(',', $ip));
    return $ip;
}
function p($str)
{
    v($str);
    exit("\n--------------------");
}
function v($str)
{
    echo "<pre>";
    echo "-------------------------\n";
    print_r($str);
}
//得到来访者浏览器
function fw_getUA () {
    $Agent=$_SERVER['HTTP_USER_AGENT'];;
    $browseragent="";   //浏览器
    $browserversion=""; //浏览器的版本
    if (ereg('MSIE ([0-9].[0-9]{1,2})',$Agent,$version)) {
        $browserversion=$version[1];
        $browseragent="Internet Explorer";
    } else if (ereg( 'Opera/([0-9]{1,2}.[0-9]{1,2})',$Agent,$version)) {
        $browserversion=$version[1];
        $browseragent="Opera";
    } else if (ereg( 'Firefox/([0-9.]{1,5})',$Agent,$version)) {
        $browserversion=$version[1];
        $browseragent="Firefox";
    }else if (ereg( 'Chrome/([0-9.]{1,3})',$Agent,$version)) {
        $browserversion=$version[1];
        $browseragent="Chrome";
    }
    else if (ereg( 'Safari/([0-9.]{1,3})',$Agent,$version)) {
        $browseragent="Safari";
        $browserversion="";
    }
    else {
        $browserversion="";
        $browseragent="Unknown";
    }
    return $browseragent." ".$browserversion;
}
//得到来访者操作系统
function fw_getOS () {
    $Agent=strtolower($_SERVER['HTTP_USER_AGENT']);;
    $browserplatform='';
    if (eregi('win',$Agent) && strpos($Agent, '95')) {
        $browserplatform="Windows 95";
    } elseif(strpos($Agent, 'ipod')) {
        $browserplatform = 'ipod';
    } elseif(strpos($Agent, 'ipad')) {
        $browserplatform = 'ipad';
    } elseif(strpos($Agent, 'iphone')) {
        $browserplatform = 'iphone';
    } elseif (strpos($Agent, 'android')) {
        $browserplatform = 'android';
    }
    elseif (eregi('win 9x',$Agent) && strpos($Agent, '4.90')) {
        $browserplatform="Windows ME";
    }
    elseif (eregi('win',$Agent) && ereg('98',$Agent)) {
        $browserplatform="Windows 98";
    }
    elseif (eregi('win',$Agent) && eregi('nt 5.0',$Agent)) {
        $browserplatform="Windows 2000";
    }
    elseif (eregi('win',$Agent) && eregi('nt 5.1',$Agent)) {
        $browserplatform="Windows XP";
    }
    elseif (eregi('win',$Agent) && eregi('nt 6.0',$Agent)) {
        $browserplatform="Windows Vista";
    }
    elseif (eregi('win',$Agent) && eregi('nt 6.1',$Agent)) {
        $browserplatform="Windows 7";
    }
    elseif (eregi('win',$Agent) && eregi('nt 10.',$Agent)) {
        $browserplatform="Windows 10";
    }
    elseif (eregi('win',$Agent) && ereg('32',$Agent)) {
        $browserplatform="Windows 32";
    }
    elseif (eregi('win',$Agent) && eregi('nt',$Agent)) {
        $browserplatform="Windows NT";
    }elseif (eregi('Mac OS',$Agent)) {
        $browserplatform="Mac OS";
    }
    elseif (eregi('linux',$Agent)) {
        $browserplatform="Linux";
    }
    elseif (eregi('unix',$Agent)) {
        $browserplatform="Unix";
    }
    elseif (eregi('sun',$Agent) && eregi('os',$Agent)) {
        $browserplatform="SunOS";
    }
    elseif (eregi('ibm',$Agent) && eregi('os',$Agent)) {
        $browserplatform="IBM OS/2";
    }
    elseif (eregi('Mac',$Agent) && eregi('PC',$Agent)) {
        $browserplatform="Macintosh";
    }
    elseif (eregi('PowerPC',$Agent)) {
        $browserplatform="PowerPC";
    }
    elseif (eregi('AIX',$Agent)) {
        $browserplatform="AIX";
    }
    elseif (eregi('HPUX',$Agent)) {
        $browserplatform="HPUX";
    }
    elseif (eregi('NetBSD',$Agent)) {
        $browserplatform="NetBSD";
    }
    elseif (eregi('BSD',$Agent)) {
        $browserplatform="BSD";
    }
    elseif (ereg('OSF1',$Agent)) {
        $browserplatform="OSF1";
    }
    elseif (ereg('IRIX',$Agent)) {
        $browserplatform="IRIX";
    }
    elseif (eregi('FreeBSD',$Agent)) {
        $browserplatform="FreeBSD";
    }
    if ($browserplatform=='') {$browserplatform = "Unknown"; }
    return $browserplatform;
}
#--------------------------------------------函数区 end---------------------------------------------
